book3s_64_mmu_hv.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
  41. #define MAX_LPID_970 63
  42. #define NR_LPIDS (LPID_RSVD + 1)
  43. unsigned long lpid_inuse[BITS_TO_LONGS(NR_LPIDS)];
  44. long kvmppc_alloc_hpt(struct kvm *kvm)
  45. {
  46. unsigned long hpt;
  47. unsigned long lpid;
  48. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN,
  49. HPT_ORDER - PAGE_SHIFT);
  50. if (!hpt) {
  51. pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n");
  52. return -ENOMEM;
  53. }
  54. kvm->arch.hpt_virt = hpt;
  55. do {
  56. lpid = find_first_zero_bit(lpid_inuse, NR_LPIDS);
  57. if (lpid >= NR_LPIDS) {
  58. pr_err("kvm_alloc_hpt: No LPIDs free\n");
  59. free_pages(hpt, HPT_ORDER - PAGE_SHIFT);
  60. return -ENOMEM;
  61. }
  62. } while (test_and_set_bit(lpid, lpid_inuse));
  63. kvm->arch.sdr1 = __pa(hpt) | (HPT_ORDER - 18);
  64. kvm->arch.lpid = lpid;
  65. pr_info("KVM guest htab at %lx, LPID %lx\n", hpt, lpid);
  66. return 0;
  67. }
  68. void kvmppc_free_hpt(struct kvm *kvm)
  69. {
  70. clear_bit(kvm->arch.lpid, lpid_inuse);
  71. free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
  72. }
  73. void kvmppc_map_vrma(struct kvm *kvm, struct kvm_userspace_memory_region *mem)
  74. {
  75. unsigned long i;
  76. unsigned long npages = kvm->arch.ram_npages;
  77. unsigned long pfn;
  78. unsigned long *hpte;
  79. unsigned long hash;
  80. struct kvmppc_pginfo *pginfo = kvm->arch.ram_pginfo;
  81. if (!pginfo)
  82. return;
  83. /* VRMA can't be > 1TB */
  84. if (npages > 1ul << (40 - kvm->arch.ram_porder))
  85. npages = 1ul << (40 - kvm->arch.ram_porder);
  86. /* Can't use more than 1 HPTE per HPTEG */
  87. if (npages > HPT_NPTEG)
  88. npages = HPT_NPTEG;
  89. for (i = 0; i < npages; ++i) {
  90. pfn = pginfo[i].pfn;
  91. if (!pfn)
  92. break;
  93. /* can't use hpt_hash since va > 64 bits */
  94. hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK;
  95. /*
  96. * We assume that the hash table is empty and no
  97. * vcpus are using it at this stage. Since we create
  98. * at most one HPTE per HPTEG, we just assume entry 7
  99. * is available and use it.
  100. */
  101. hpte = (unsigned long *) (kvm->arch.hpt_virt + (hash << 7));
  102. hpte += 7 * 2;
  103. /* HPTE low word - RPN, protection, etc. */
  104. hpte[1] = (pfn << PAGE_SHIFT) | HPTE_R_R | HPTE_R_C |
  105. HPTE_R_M | PP_RWXX;
  106. wmb();
  107. hpte[0] = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
  108. (i << (VRMA_PAGE_ORDER - 16)) | HPTE_V_BOLTED |
  109. HPTE_V_LARGE | HPTE_V_VALID;
  110. }
  111. }
  112. int kvmppc_mmu_hv_init(void)
  113. {
  114. unsigned long host_lpid, rsvd_lpid;
  115. if (!cpu_has_feature(CPU_FTR_HVMODE))
  116. return -EINVAL;
  117. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  118. if (cpu_has_feature(CPU_FTR_ARCH_206)) {
  119. host_lpid = mfspr(SPRN_LPID); /* POWER7 */
  120. rsvd_lpid = LPID_RSVD;
  121. } else {
  122. host_lpid = 0; /* PPC970 */
  123. rsvd_lpid = MAX_LPID_970;
  124. }
  125. set_bit(host_lpid, lpid_inuse);
  126. /* rsvd_lpid is reserved for use in partition switching */
  127. set_bit(rsvd_lpid, lpid_inuse);
  128. return 0;
  129. }
  130. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  131. {
  132. }
  133. static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
  134. {
  135. kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
  136. }
  137. static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  138. struct kvmppc_pte *gpte, bool data)
  139. {
  140. return -ENOENT;
  141. }
  142. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  143. {
  144. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  145. if (cpu_has_feature(CPU_FTR_ARCH_206))
  146. vcpu->arch.slb_nr = 32; /* POWER7 */
  147. else
  148. vcpu->arch.slb_nr = 64;
  149. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  150. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  151. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  152. }